package chatManagers;

//package wmmd.chat;
//package chatManagers;

/*
 * getChatList.java
 *
 * Used only in the admin section...returns a list of chat names, descriptions,
 * start & end dates, and links to transcripts of each chat.
 *
 */

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class getChatList extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //DBMSManager dbm= new DBMSManager(request.getParameter("db"));
        DBMSManager dbm = new DBMSManager();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        int uid;
        ResultSet rs = null;
        
        rs = dbm.getAllChats();
        try {
            int i=0;
            while(rs.next()) {
                String ownerStr = rs.getString("owner");
                int id = rs.getInt("id");
                java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MMM d, yyyy");
                java.text.SimpleDateFormat tf = new java.text.SimpleDateFormat("hh:mm aa");
                String nameStr = rs.getString("name");
                String descStr = rs.getString("description");

                // For formatting the start and end times of the chat
                Timestamp tStamp = rs.getTimestamp("startDate");
                String sDate = df.format(tStamp);
                String sTime = tf.format(tStamp);
                String eTime = tf.format(rs.getTime("endDate"));
                
                if(dbm.isChatActive(id)) {
                    // Housekeeping - close chats that need to be closed.
                }
                out.print("<tr><td>" + nameStr + "</td>");
                out.print("<td>" + sDate + "</td>");
                out.print("<td>" + sTime + "</td><td>");
                if (sTime.equals(eTime)) {
                    out.print("<b><span style=\"color:009900;\">In Progress</span></b>");
                } else {
                    out.print(eTime);
                }
                out.print("</td><td><a href=\"ViewTranscript.jsp?cid=" + id + "\">");
                out.println("View Transcript</a></td></tr>");
                i++;
            }
            rs.close();
            if(i==0) {
                out.println("<span style=\"color:ff0000\"><b>Sorry</b></span>, but " +
                        "there are no chats available for you to view.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally
        {dbm.closeConnection();}
    }
    
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
// </editor-fold>
}
